<?PHP

/**
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
 * @author Chris Reekie
 * @version 4/20/2011
 *
 * CIS 605
 * Donor Individual View
 */
/**
 * Displays Donors that fit search 
 */
include_once('\../db-control/dbDonor2.php');

if ($_POST['table'] == '1') {
    $selectInd = "selected";
    $selectOrg = "";
} elseif ($_POST['table'] == '2') {
    $selectInd = "";
    $selectOrg = "selected";
} else {
    $selectInd = "";
    $selectOrg = "";
}

//echo "selectInd has: $selectInd and selectOrg has: $selectOrg<br />"; debug

echo "<form name='searchDonors' method='post' action='searchDonors.php'>
 <table width='60%'>
   <tr>
   <td>
   <h3>Enter an individuals last name or an organization name.</h3>
   </td>
   </tr>
   <tr>
     <td>
       Search:<input type='text' name='donorName' size='30' maxlength='35'> in 
       <select name='table' size='1'>
         <option value='1'" . $selectInd . ">Individual Donor
         <option value='2'" . $selectOrg . ">Organization
       </select>
       <input type='submit' value='Find'>
     </td>
   </tr>
 </table>
</form>";
$donorName = $_POST['donorName'];
$tableselect = $_POST['table'];

//echo $donorName . "<br/>"; debug
//echo $_POST['table'] . "<br/>"; debug
if (!isset($_POST['donorName']) || $_POST['donorName'] == "") {
    echo "Enter name of Donor or Donor Organization.";
}
switch ($tableselect) {
    case 1:
        $indvDonor = searchIndvByLast($donorName);
        if ($indvDonor != false) {
            echo "<h2>Individual Donor</h2>
    <table border='3' width='80%' >
<tr>
<th> Donor Name</th>
<th> e-mail</th>
<th> Phone Number</th>
<th> Address</th>
<th> Update</th>
</tr>";
            while ($row = mysql_fetch_array($indvDonor)) {
                $dID = $row["donorID"];
                $firstName = $row["firstName"];
                $lastName = $row["lastName"];
                $email = $row["email"];
                $phone = $row["phone"];
                $street = $row["street"];
                $city = $row["city"];
                $state = $row["stateName"];
                $zip = $row["zip"];
				
				$_POST['donorID']= $dID;
				$_POST['firstName']=$lastName;
				$_POST['lastName']=$firstName;
				$_POST['email']=$email;
				$_POST['phone']=$phone;
				$_POST['street']=$street;
				$_POST['city']=$city;
				$_POST['stateName']=$stateName;
				$_POST['zip']=$zip;
				
				
echo $dID;
                echo "<tr>
         <td>" . $firstName . " " . $lastName . "</td>
         <td>" . $email . "</td>
         <td>" . $phone . "</td>
         <td>" . $street . " " . $city . " " . $state . ", " . $zip . "</td>
		 <td> <form method='post' action='searchDonors.php'>
		 <INPUT TYPE='hidden' name='ID' VALUE=$dID> 
		 <input type='submit' value='Select'>
		 </form></td> </tr>";// Problem with line 87 cant pass var this way?
                //enter form to have one button "Select" 
                //hidden input with $donorID
		}
            echo "</table> ";
        } else
            echo "</br> Error, no donor by that name. <br/>";
        break;
    case 2:
        $orgDonor = searchOrgByOrgName($donorName);

        if ($orgDonor != false) {
            echo "<h2>Organization Donor</h2>
    <table border='3' width='80%'  >
<tr>
<th> Donor Name</th>
<th> e-mail</th>
<th> Phone Number</th>
<th> Address</th>
</tr>";
            while ($row = mysql_fetch_array($orgDonor)) {
                $dID = $row["donorID"];
                $dName = $row["organizationName"] . "; " . $row["description"];
                $email = $row["email"];
                $phone = $row["phone"];
                $address = $row["street"] . " " . $row["city"] . ", " . $row["stateName"] . " " . $row["zip"];

                echo "<tr>";
                echo "<td>" . $dName . "</td>";
                echo "<td>" . $email . "</td>";
                echo "<td>" . $phone . "</td>";
                echo "<td>" . $address . "</td>";
                echo "</tr>";
            }
            echo "</table> ";
        } else
            echo "Error, no organization donor by that name. <br/>";
}
if (isset($_POST['ID']))
{
$ID=$_POST['ID'];
//echo $ID;
$byID=searchIndvByID($ID);
$row=mysql_fetch_assoc($byID);
				$dID = $row["donorID"];
                $firstName = $row["firstName"];
                $lastName = $row["lastName"];
                $email = $row["email"];
                $phone = $row["phone"];
				$fax = $row["fax"];
                $street = $row["street"];
                $city = $row["city"];
                $state = $row["stateName"];
                $zip = $row["zip"];
				//echo $firstName;
				$_POST['donorID']= $dID;
				$_POST['firstName']=$lastName;
				$_POST['lastName']=$firstName;
				$_POST['email']=$email;
				$_POST['phone']=$phone;
				$_POST['fax']=$fax;
				$_POST['street']=$street;
				$_POST['city']=$city;
				$_POST['stateName']=$stateName;
				$_POST['zip']=$zip;
				
echo"<h3>Update Donor</h3>
    
    <form name='searchDonors' method='post' action='searchDonors.php'>
               <table border='3' width='50%'  >
                        <tr>
                            <td width='300px'>
                                First Name: <input type='text' name='firstName'
                                        value = '$firstName'        
                                        style='width:100%'/> </td>
                            <td width='300px'>
                                Last Name: <input type='text' name='lastName'
                                        value = '$lastName' 
                                        style='width:100%'/> </td> </tr>
                        <tr>
                            <td width='100px'>
                                e-mail: <input type='text' name='email' 
                                        value = '$email'
                                        style='width:100%'/> </td> </tr>
						<tr>
                            <td width='100px'>
                                Phone: <input type='text' name='phone' 
                                        value = '$phone'
                                        style='width:100%'/> </td>
							<td width='100px'>
                                Fax: <input type='text' name='fax' 
                                        value = '$fax'
                                        style='width:100%'/> </td> </tr>
                        <tr>
                            <td>
                                Address: <input type='text' name='street' 
                                        value = '$street'
                                        style='width:100%'/> </td>
                            <td>
                                City: <input type='text' name='city' 
                                        value = '$city'
                                         style='width:100%'/> </td>
                            <td width='150px'>
                                State: <input type='text' maxlength='2'
                                        size='2' name='state'
										value = '$state'
                                        style='width:100%'/> </td>
                            <td width='150px'>
                                Zip: <input type='text' maxlength='5'
                                        style='width:100%' size='5' name='zip'
										value = '$zip'/> </td> </tr>
                        <tr>
                            <td colspan='4'>
                                <input type='submit' name='update'
                                value='Update' style='width:125px' />
            </table>";
			}